package com.jbp.controller;

import com.jbp.domin.LuckReq2;
import com.jbp.domin.LuckTestReq;
import com.jbp.model.User;
import com.jbp.model.UserDailyLuckRecord;
import com.jbp.model.Wallet;
import com.jbp.service.LoginService;
import com.jbp.service.LotteryService;
import com.jbp.service.UserDailyLuckRecordService;
import com.jbp.service.WalletService;
import com.jbp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

@RestController
@RequestMapping(value = "luckTest")
public class TestLuckController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private LotteryService lotteryService;
    @Autowired
    private UserDailyLuckRecordService userDailyLuckRecordService;

    @PostMapping("/win")
    public R win(@RequestBody LuckTestReq req){
        for (int i = 0; i < req.getUserCount(); i++) {
            //创建user 给心钻 105000
            User user = new User();
            user.setMobile("100000000"+ new Random().nextInt(90000000)+i);
            user.setName(new Random().nextInt(999999)+""+i);
            loginService.createUser(user);

            Wallet wallet = walletService.getByUserId(user.getId());
            wallet.setXinzuan(BigDecimal.valueOf(req.getMoney()));
            walletService.updateWithLock(wallet);

            LuckReq2 luckReq2 = new LuckReq2();
            luckReq2.setPrice(req.getPrice());
            luckReq2.setUserId(user.getId());
            luckReq2.setTimes(req.getTimes());
            luckReq2.setRoomId(11);

            for (int j = 0; j < req.getLuckTimes(); j++) {
                lotteryService.luck2(luckReq2);
                HashMap<String, BigDecimal> record = userDailyLuckRecordService.getLuckByType(user.getId(), req.getPrice());
                BigDecimal pay = record.get("pay");
                BigDecimal income = record.get("income");
                //赚钱就停
                if (income.multiply(BigDecimal.valueOf(0.85d)).doubleValue() > pay.doubleValue()) {
                    break;
                }
            }

            HashMap<String, BigDecimal> record = userDailyLuckRecordService.getLuckByType(user.getId(), req.getPrice());
            BigDecimal pay = record.get("pay");
            BigDecimal income = record.get("income");
            double rate = income.divide(pay, 4, 2).doubleValue();
            System.out.println(i+": "+" 投入="+pay+" 产出="+income+" 爆率="+rate);
        }
        return R.ok();
    }

    @PostMapping("/luck")
    public R luck(@RequestBody LuckTestReq req){
        for (int i = 0; i < req.getUserCount(); i++) {
            //创建user 给心钻 105000
            User user = new User();
            user.setMobile("100000000"+ new Random().nextInt(90000000)+i);
            user.setName(new Random().nextInt(999999)+""+i);
            loginService.createUser(user);

            Wallet wallet = walletService.getByUserId(user.getId());
            wallet.setXinzuan(BigDecimal.valueOf(req.getMoney()));
            walletService.updateWithLock(wallet);

            LuckReq2 luckReq2 = new LuckReq2();
            luckReq2.setPrice(req.getPrice());
            luckReq2.setUserId(user.getId());
            luckReq2.setTimes(req.getTimes());
            luckReq2.setRoomId(1);

            for (int j = 0; j < req.getLuckTimes(); j++) {
                System.out.println(i+"  "+j);
                lotteryService.luck2(luckReq2);

            }
        }
        return R.ok();
    }
}
